package no.saiboten.drumcalendar.controller;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import no.saiboten.drumcalendar.day.DayService;
import no.saiboten.drumcalendar.user.CalendarUser;
import no.saiboten.drumcalendar.user.CalendarUserService;
import no.saiboten.drumcalendar.user.LoggedInRequestHolder;
import no.saiboten.drumcalendar.user.UserStatistics;
import no.saiboten.drumcalendar.user.WinnerService;
import no.saiboten.drumcalendar.utils.StatisticsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@Controller
public class DefaultController {

	private DayService dayService;

	private LoggedInRequestHolder loggedIn;

	private CalendarUserService userService;

	private StatisticsService statsService;

	private WinnerService winnerService;

	@Autowired
	public DefaultController(DayService dayService, LoggedInRequestHolder loggedIn, CalendarUserService userService,
			StatisticsService statsService, WinnerService winnerService) {
		this.dayService = dayService;
		this.loggedIn = loggedIn;
		this.userService = userService;
		this.statsService = statsService;
		this.winnerService = winnerService;
	}

	@RequestMapping("")
	public ModelAndView start() {
		ModelAndView mav = new ModelAndView("main");

		mav.addObject("now", Calendar.getInstance().getTimeInMillis());

		CalendarUser user = loggedIn.getCalendarUser();

		mav.addObject("today", dayService.getToday());
		mav.addObject("loggedIn", loggedIn.isLoggedIn());
		mav.addObject("user", user);

		List<CalendarUser> bestUsers = statsService.getBestUsers();
		if (bestUsers != null && bestUsers.size() > 0) {
			mav.addObject("bestUsers", bestUsers);
		}

		if (user != null) {
			UserStatistics userStats = userService.getUserStatistics(user.getUserName());
			mav.addObject("userStats", userStats);
		}

		mav.addObject("winners", winnerService.getWinners());

		return mav;
	}

	@RequestMapping("/om")
	public String om() {
		return "om";
	}

	@RequestMapping("/overview")
	public ModelAndView overview() {
		ModelAndView mav = new ModelAndView("overview");
		mav.addObject("now", Calendar.getInstance().getTimeInMillis());
		mav.addObject("days", dayService.getDays());
		mav.addObject("loggedIn", loggedIn.isLoggedIn());
		if (loggedIn.getCalendarUser() != null) {
			mav.addObject("answers", loggedIn.getCalendarUser().getAnswers());
		}

		List<CalendarUser> users = userService.getAllUsers();
		if (users != null) {
			mav.addObject("numberOfUsers", users.size());
		}

		mav.addObject("statistics", statsService.getStatistics());
		return mav;
	}

	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("login");
		String thisUri = request.getRequestURI();
		UserService userService = UserServiceFactory.getUserService();

		if (loggedIn.isLoggedIn()) {
			mav.addObject("loggedIn", true);
			mav.addObject("logoutUrl", userService.createLogoutURL(thisUri));
		} else {
			mav.addObject("loggedIn", false);
			mav.addObject("loginUrl", userService.createLoginURL(thisUri));
		}

		return mav;
	}

	@ExceptionHandler
	public String error() {
		return "error";
	}

}
